use derive_builder::Builder;
use serde::{Deserialize, Serialize};

use crate::error::Error;
use crate::mapper::ValueMapper;
use crate::modeling::{
    Class, ClassAttributeBuilder, ClassBuilder, ClassGetter, StorageStatus, ValueType,
};

use crate::modeling::{
    ClassAttributePersistence, ClassPersistence, ClassPersistenceBuilder, ClassPersistenceGetter,
    ClassRelation, ClassRelationGetter, Value,
};

const APPSTORE_APPLICATION_VERSION_CLASS_IDENTIFIER: &'static str =
    "g01_appstore_application_version";

const APPSTORE_APPLICATION_VERSION_CLASS_NAME: &'static str = "appstore_application_version";

const APPSTORE_APPLICATION_VERSION_TABLE_NAME: &'static str = "_g01_appstore_application_versions";

#[derive(Debug, Serialize, Deserialize, Default, Clone, Builder, PartialEq)]
#[builder(name = "AppStoreApplicationVersionBuilder")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
#[builder(derive(Debug))]
#[builder(build_fn(error = "Error"))]
pub struct AppStoreApplicationVersion {
    /// id
    pub id: i64,
    /// identifier
    pub identifier: String,
    /// version
    pub version: String,
    /// download
    pub download: String,
    /// is_public
    pub is_public: bool,
    /// 备注
    pub description: String,
    /// 创建时间
    pub created_at: i64,
    /// 更新时间
    pub updated_at: i64,
    /// status
    pub status: StorageStatus,
}

//
crate::impl_from_value!(AppStoreApplicationVersion);
crate::impl_class_persistence_getter!(
    AppStoreApplicationVersion,
    APPSTORE_APPLICATION_VERSION_TABLE_NAME
);
crate::impl_class_relations_getter!(AppStoreApplicationVersion, vec![]);

impl ClassGetter for AppStoreApplicationVersion {
    fn get_name() -> anyhow::Result<String> {
        Ok(String::from(APPSTORE_APPLICATION_VERSION_CLASS_NAME))
    }
    fn get_identifier() -> anyhow::Result<String> {
        Ok(String::from(APPSTORE_APPLICATION_VERSION_CLASS_IDENTIFIER))
    }
    fn get_class() -> anyhow::Result<Class> {
        let mut attribute_builder = ClassAttributeBuilder::default();
        attribute_builder.class_identifier(APPSTORE_APPLICATION_VERSION_CLASS_IDENTIFIER);

        let mut commons = crate::modeling::simple_attributes_with_builder(&attribute_builder)?;
        let mut append = vec![
            attribute_builder
                .clone()
                .name("version")
                .title("version")
                .data_type(ValueType::String)
                .build()?,
            attribute_builder
                .clone()
                .name("identifier")
                .title("identifier")
                .data_type(ValueType::String)
                .build()?,
            attribute_builder
                .clone()
                .name("download")
                .title("download")
                .data_type(ValueType::String)
                .build()?,
            attribute_builder
                .clone()
                .name("is_public")
                .title("is_public")
                .default_value("0")
                .storage_type(ValueType::Number)
                .data_type(ValueType::Boolean)
                .build()?,
            attribute_builder
                .clone()
                .name("description")
                .title("description")
                .default_value("")
                .data_type(ValueType::String)
                .build()?,
            attribute_builder
                .clone()
                .name("status")
                .title("status")
                .default_value("1")
                .storage_type(ValueType::Number)
                .data_type(ValueType::String)
                .build()?,
        ];
        append.append(&mut commons);
        append.sort_by(|a, b| a.position.cmp(&b.position));

        let clazz = ClassBuilder::default()
            .identifier(APPSTORE_APPLICATION_VERSION_CLASS_IDENTIFIER)
            .name(APPSTORE_APPLICATION_VERSION_CLASS_NAME)
            .data_is_type(false)
            .is_persistent(true)
            .primary_key(String::from("identifier"))
            .attributes(append)
            .build()?;
        Ok(clazz)
    }
}
